AmazonCovers = {}
AmazonCovers = function()
{
  this.search_result;
  this.ajax =  new Ajax();//(typeof ajax == 'object')? ajax : new Ajax();
  this.target;
  this.result_num = 0;
  this.current_cover = 0;
  this.attempts = 0;
  this.data = null;
  this.album = false;
  this.asin;
  
  window.cover = this;
}

AmazonCovers.inherits(WebService);

AmazonCovers.prototype.search = function(artist, title)
{
  if(!this.data)
    this.data = new Array(artist,title);
  
  var art = encodeURIComponent(artist);
  var tle = encodeURIComponent(title);
  var c = this;
  var options = {target: c.target, timeout: 0, 
          onLoading:function(o,target){ /*onloading(target,1);*/},
          onComplete:function(o,target){c.search_result = o.responseXML; c.attempts++; var cnt = c.show(); if(cnt) target.innerHTML = cnt;},        
          data: (this.asin?'asin='+this.asin+'&':'')+'artist='+art+'&title='+(this.album?this.album:tle)+'&op=covers'};

  this.ajax.makeRequest('proxy.php', options);

} 

AmazonCovers.prototype.show = function()
{ 
  try { var req = this.search_result.getElementsByTagName('Request')[0].getElementsByTagName('IsValid')[0].firstChild.nodeValue; }
  catch(e) { this.error() }
  if(req != 'True') {
    this.attempts = 3;
  }
  else{
    var cover = this.search_result.getElementsByTagName('Item');
    try{
      
    //this.result_num = this.search_result.getElementsByTagName('TotalResults')[0].firstChild.nodeValue;
    //this.total_pages = this.search_result.getElementsByTagName('TotalPages')[0].firstChild.nodeValue;

    for(var i=0; i < cover.length; i++){ 
      var src = cover[i].getElementsByTagName('MediumImage')[0].getElementsByTagName('URL')[0].firstChild.nodeValue;
        try{
          var h = cover[i].getElementsByTagName('MediumImage')[0].getElementsByTagName('Heigth')[0].firstChild.nodeValue+'px';
          var w = cover[i].getElementsByTagName('MediumImage')[0].getElementsByTagName('Width')[0].firstChild.nodeValue+'px';
        }catch(e){var w = 'auto'; var h = 'auto';}
        
        this.next_cover= (i == (cover.length+1) ? false: i+1);
        
        // cache the img
        var options = {target: null, timeout: 0, 
          onLoading:function(){ },
          onComplete:function(){ },        
          data: 'artist='+this.data[0]+'&title='+this.data[1]+(this.album?'&album='+this.album:'')+'&src='+src+'&op=cache_covers'};

          new Ajax().makeRequest('proxy.php', options);
          
        return '<img style="width:'+w+';height:'+h+';" src="'+src+'" alt="Album cover" title="'+(this.album?this.album:this.data[1])+', by '+this.data[0]+'"/>';
      } 
    } 
    catch(e) {  }
  }
                                                        
  if(this.attempts < 3){
    this.asin = null;
    this.ajax = new Ajax();
    if(this.attempts < 2) this.search(this.data[0],this.data[1]);   
    else this.search(this.data[0],''); 
    return 0;
  }
  return '<img src="http://ec1.images-amazon.com/images/G/01/x-site/icons/no-img-lg._V47059465_.gif" alt="no cover found :(" />';
  
}

AmazonCovers.CB =  function(dataObj) { 
  var self = window.cover; 
  try { self.album = dataObj.ResultSet.Result[0].Album.content; self.yahoo_id=dataObj.ResultSet.Result[0].id; }
  catch(e){ }
  self.search(self.data[0],self.album?dataObj.ResultSet.Result[0].Album.content:'');
}

